﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using TPCN.DAL;
using TPCN.Commons;

namespace TPCN
{
    public partial class Tinhtrangdonhang : System.Web.UI.Page
    {
        private static string userID = String.Empty;
        private static List<DonDatHangObj> list;

        private static bool isConfirm = false;
        private static bool isDelivering = false;
        private static bool isDelivered = false;
        private static bool isPaid = false;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (HttpContext.Current.Session["user"] == null)
            {
                Response.Redirect("~");
            }
            else
            {
                userID = HttpContext.Current.Session["user"].ToString();
                list = new List<DonDatHangObj>();
                list = DonDatHangDAL.GetByKhachID(userID);
                gridOrder.DataSource = list;
                gridOrder.DataBind();
            }

        }

        protected string GetStatus(Object OrderID)
        {
            string mID = Convert.ToString(OrderID);
            string result = String.Empty;
            isConfirm = false;
            isDelivering = false;
            isDelivered = false;
            isPaid = false;
            List<DonHangTrangThaiDonHangObj> l = new List<DonHangTrangThaiDonHangObj>();
            l = DonHangTrangThaiDonHangDAL.GetByOrder(mID);
            for (int i = 0; i < l.Count; i++)
            {
                if (l[i].TrangthaiID == Const.KEY_STATUS_CONFIRMED)
                    isConfirm = true;
                else if (l[i].TrangthaiID == Const.KEY_STATUS_DELIVERING)
                    isDelivering = true;
                else if (l[i].TrangthaiID == Const.KEY_STATUS_DELIVERED)
                    isDelivered = true;
                else if (l[i].TrangthaiID == Const.KEY_STATUS_PAID)
                    isPaid = true;
            }
            result = "Chưa xác nhận";
            if (isConfirm)
                result = "Đã xác nhận";
            if (isDelivering)
                result = "Đang giao hàng";
            if (isPaid)
                result = "Đã thu tiền";
            if (isDelivered)
                result = "Đã giao hàng";
            return result;
        }

        protected string FormatDate(object _date)
        {
            DateTime date = Convert.ToDateTime(_date);
            return String.Format("{0:dd/MM/yyyy}", date);
        }

        protected void gridOrder_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            string ID = e.CommandArgument.ToString();
            if (e.CommandName == "cmdChitiet")
            {
                gridOrder.Visible = false;
                ChitietdondathangGridView.Visible = true;
                btnBack.Visible = true;
                LoadOrderDetail(ID);
            }
        }

        protected void LoadOrderDetail(string id)
        {
            List<ChiTietDonDatHangObj> list = ChiTietDonDatHangDAL.GetByOrderID(id);
            ChitietdondathangGridView.DataSource = list;
            ChitietdondathangGridView.DataBind();
        }

        protected void btnBack_Click(object sender, EventArgs e)
        {
            ChitietdondathangGridView.Visible = false;
            gridOrder.Visible = true;
            btnBack.Visible = false;
        }

        protected void ChitietdondathangGridView_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            ChitietdondathangGridView.PageIndex = e.NewPageIndex;
            ChitietdondathangGridView.DataBind();
        }

        protected void gridOrder_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gridOrder.PageIndex = e.NewPageIndex;
            gridOrder.DataBind();
        }

        protected string GetProductNameFromID(Object id)
        {
            string mID = Convert.ToString(id);
            return HangDAL.GetNameByID(mID);
        }
    }
}